package org.apache.commons.logging.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogConfigurationException;
import org.apache.commons.logging.ILogLevel.LevelConstant;

import com.google.gwt.core.client.GWT;
import com.googlecode.gwt.commons.logging.impl.LogTimer;

public class ServerLogFactoryImpl extends LogFactoryImpl {
	
	
	public static LogTimer loggerServer = new LogTimer() {
		public LevelConstant getLevelConstant(int level) {
			LevelConstant temp = null;

			if (level == SEVERE.getIntValue()) {
				temp = SEVERE;
			} else if (level == WARNING.getIntValue()) {
				temp = WARNING;
			} else if (level == INFO.getIntValue()) {
				temp = INFO;
			} else if (level == ALL.getIntValue()) {
				temp = ALL;
			} else if (level == FINE.getIntValue()) {
				temp = FINE;
			} else if (level == FINEST.getIntValue()) {
				temp = FINEST;
			} else {
				temp = OFF;
			}

			return temp;
		}
	};
	
	
	/**
	 * start to send a list of client log
	 * 
	 */
	public void start() {
		loggerServer.start();
	}

	/**
	 * Stop to send a list of client log
	 * 
	 */
	public void stop() {
		loggerServer.stop();
	}

	public class ServerLogger implements Log {

		private final Log logger;
		private final String loggerName;
		
		
		public ServerLogger(Log logger, String name) {
			this.logger=logger;
			this.loggerName=name;
		}
		
		
		public void log(LevelConstant level, String msg, Throwable ex) {

				String method = "unknown";
//				if (!isScript) {
//					StackTraceElement locations = getStackTraceElement();
//					if (locations != null) {
//						method = locations.getMethodName();
//					}
//				}
				loggerServer.addLog(level, loggerName , method, msg, ex);
		}

		public void debug(Object message) {
			logger.debug(message);
			log(loggerServer.FINE, String.valueOf(message), null);
		}

		public void debug(Object message, Throwable t) {
			logger.debug(message,t);
			log(loggerServer.FINE, String.valueOf(message), t);
		}

		public void error(Object message) {
			logger.error(message);
			log(loggerServer.SEVERE, String.valueOf(message),null);
		}

		public void error(Object message, Throwable t) {
			logger.error(message,t);
			log(loggerServer.SEVERE, String.valueOf(message), t);
		}

		public void fatal(Object message) {
			logger.fatal(message);
			log(loggerServer.SEVERE, String.valueOf(message), null);
		}

		public void fatal(Object message, Throwable t) {
			logger.fatal(message,t);
			log(loggerServer.SEVERE, String.valueOf(message), t);
		}

		public void info(Object message) {
			logger.info(message);
			log(loggerServer.INFO, String.valueOf(message), null);

		}

		public void info(Object message, Throwable t) {
			logger.info(message, t);
			log(loggerServer.INFO, String.valueOf(message), t);

		}

		public boolean isDebugEnabled() {
			return logger.isDebugEnabled();
		}

		public boolean isErrorEnabled() {
			return logger.isErrorEnabled();
		}

		public boolean isFatalEnabled() {
			return logger.isFatalEnabled();
		}

		public boolean isInfoEnabled() {
			return logger.isInfoEnabled();
		}

		public boolean isTraceEnabled() {
			return logger.isTraceEnabled();
		}

		public boolean isWarnEnabled() {
			return logger.isWarnEnabled();
		}

		public void trace(Object message) {
			logger.trace(message);
			log(loggerServer.FINEST, String.valueOf(message), null);
		}

		public void trace(Object message, Throwable t) {
			logger.trace(message, t);
			log(loggerServer.FINEST, String.valueOf(message), t);

		}

		public void warn(Object message) {
			logger.warn(message);
			log(loggerServer.WARNING, String.valueOf(message),null);

		}

		public void warn(Object message, Throwable t) {
			logger.warn(message,t);
			log(loggerServer.WARNING, String.valueOf(message), t);
		}

	}
	
	protected Log newInstance(String name) throws LogConfigurationException {
		Log logger = new ServerLogger(super.newInstance(name), name);
		
		GWT.log("server",null);
		
		return logger;
	}
}
